// 定义要存储的公共变量
var myData = {
    key: 'site3-f-ue',
    value: ''
};
var targetUrl = "https://digitalconstruction-pre.cscec3b-iti.com/";
chrome.tabs.query({ url: `${targetUrl}*` }, function(tabs) {
    if (tabs && tabs.length > 0) {
        chrome.scripting.executeScript({
            target: { tabId: tabs[0].id },
            function: function () {
                var localStorageData = JSON.stringify(localStorage);
                return localStorageData;
            }
        }).then((result) => {
            let session = JSON.parse(result[0].result)
            myData.value = session['site3-f-ue']
            chrome.storage.local.set({ myData: myData });
        }).catch((error) => {
            console.error('chrome插件: ', error);
        });
    }else{
        confirmFn()
    }
});

chrome.tabs.query({ active: true, currentWindow: true }, function(tabs) {
    if(tabs.length > 0) {
        var currentTab = tabs[0];
        var currentTabUrl = currentTab.url;
        const setToken = document.getElementById('setToken');
        if (currentTabUrl.startsWith('http://')) {
            setToken.style.display = 'block'
        }else{
            setToken.style.display = 'none'
        }
    }
});
function confirmFn(){
    var mymessage = confirm('chrome插件:是否打开`智慧工地平台(预发)`并登录以获取token')
    if(mymessage){
        chrome.tabs.create({
            url: targetUrl
        });
    }else{
        console.warn('chrome插件: token设置失败')
    }
}